﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using InfoSource.Models.objects;
using InfoSource.Data;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace InfoSource.Utilities
{
	public class Helpers
	{
		public static string UrlFromRoute(string action, string controller)
		{
			UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
			string url = urlHelper.Action(action, controller, null, HttpContext.Current.Request.Url.Scheme);
			return url;
				
		}


		/*------------------------------------------------------------------------------*/
		public static string Serialize(object value)
		{
			return new JavaScriptSerializer().Serialize(value);
		}

		public static ToutputType DeSerialize<ToutputType>(string value)
		{
			return new JavaScriptSerializer().Deserialize<ToutputType>(value);
		}

		public static List<List<LatLong>> GetCoordinatesFromKMLFile(string zipcode)
		{
               string infoSourcePath = AppDomain.CurrentDomain.BaseDirectory;
			XDocument doc = XDocument.Load(infoSourcePath + "\\app_data\\colorado.kml");

			List<XElement> placemarks = doc.Descendants("Placemark").ToList();
			List<XElement> matchedPlacemarks = placemarks.Where(pm => pm.Element("name").Value == zipcode).ToList();
			List<string> coordinates = matchedPlacemarks.Descendants("coordinates").Select(co => co.Value).ToList();

			List<string[]> pointSets = coordinates.Select(co => co.Split(' ')).ToList();
			List<List<LatLong>> latLongs = new List<List<LatLong>>();

			foreach (string[] point in pointSets)
			{
				List<LatLong> coords = new List<LatLong>();
				foreach (string coordinate in point.Where(p => !string.IsNullOrEmpty(p)))
				{
					LatLong latLng = new LatLong();
					string[] coordArr = coordinate.Split(',');
					if (coordArr.Length > 0) double.TryParse(coordArr[0], out latLng.Lng );
					if (coordArr.Length > 1) double.TryParse(coordArr[1], out latLng.Lat);

					coords.Add(latLng);

				}

				latLongs.Add(coords);
			}


			return latLongs;
		}

		/*------------------------------------------------------------------------------*/
        public static int SendEmail(string[] to, string subject, string content){
            try
            {
                MailMessage msg = new MailMessage();
                foreach (string recipient in to)
                {
                    msg.To.Add(new MailAddress(recipient));
                }

                msg.Subject = subject;
                msg.From = new MailAddress("AppointmentConfirmation@XcelEnergy.com");
                msg.Body = content;
                msg.IsBodyHtml = true;


                SmtpClient client = new SmtpClient();
                client.Host = "smtp.xcelenergy.com";
                client.Port = 25;
                 /*
                client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                 */

                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                //client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(msg);

                return 1;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

	}
}